/*
Prepared data fro descriptive table 
*/
/*-------------------------------
** Create INcome information 
----------------------------------*/
use "$enahoData/2019/enaho01-2019-200.dta", clear
gen period = "2019" 
tempfile dems 
sa `dems'

use "$enahoData/2019/enaho01a-2019-300.dta", clear
gen period = "2019" 
tempfile edu
sa `edu'


use "$enahoData/2019/sumaria-2019.dta", clear
gen period = "2019" 
keep mieperho  conglome vivienda hogar   period 
tempfile sumaria
sa `sumaria'



use "$enahoData/enaho_2020_tri2-Modulo05/enaho01a_2020_500.dta", clear
append using "$enahoData/enaho_2020_tri3-Modulo05/enaho01a_2020_500.dta", gen(tri3)
append using "$enahoData/enaho_2019-Modulo05/enaho01a-2019-500.dta", gen(y19)

gen days = 30 if p523 == 1
replace days = 30/7 if p523 == 2
replace days = 30/15 if p523 == 3
replace days = 1 if p523 == 4

gen earnings = p524a1 *days

gen period = "2019" if y19 ==1
replace period = "2020Q3" if tri3 ==1
replace period = "2020Q2" if  period == ""



merge 1:1 conglome vivienda hogar  codperso period  using `dems', gen(dem_merge)

merge 1:1 conglome vivienda hogar codperso period using `edu', gen(edu_merge) 

merge m:1 conglome vivienda hogar  period using `sumaria', gen(sum_merge) 


	preserve
	
		egen hogar_id = concat(nconglome conglome vivienda hogar ubigeo)
		
		//p203 == 3 for child, and limit age  to less than 17 
		g child = 1 if p203 == 3 & p208a <=17
		
		bysort hogar_id: egen n_children = total(child)
		
		duplicates drop hogar_id, force
		
		keep nconglome conglome vivienda hogar ubigeo hogar_id n_children
		
		duplicates drop hogar_id, force
		
		tempfile enaho_nchild
		save `enaho_nchild'
	
	restore
	
	preserve
	
		egen hogar_id = concat(nconglome conglome vivienda hogar ubigeo)
		egen person_id=concat(hogar_id codperso)
		
		*Men
		keep if p207 == 1
		*HH heads and partners
		keep if p203 == 1 | p203 == 2
	
		g p208_partner = p208a
		
		keep nconglome conglome vivienda hogar ubigeo hogar_id person_id p208_partner
		
		duplicates drop hogar_id, force
		
		*A data base of all male hh headas and partners age
		tempfile enaho_age_partner
		save `enaho_age_partner'
	
	restore



**
egen hogar_id=concat(nconglome conglome vivienda hogar ubigeo)
egen person_id=concat(hogar_id codperso)


***Restringiendo la muestra.
*Only Urban hh
gen urban = (estrato >= 1 & estrato <= 5) if !missing(estrato)
keep if urban == 1

*Only hh head and partner
keep if p203==1 | p203==2

//get household income FOR COUPLES 
bys conglome vivienda hogar  period: egen hh_income_couples = sum(earnings) 

*Only women
keep if p207 == 2

*18 - 45 yo
keep if p208a >= 18 & p208a <= 45

*Only partnered women
keep if p209==1 | p209==2




bys period: su hh_income*

//there are a couple fo same sex couples (nice!) within same housheold. Dont report a head of household. 
//just randomly keep one of them. Only like 10 obs so not a big deal 

sort hogar_id, stable
by hogar_id: gen n = _n 
drop if n > 1
	
merge 1:1 hogar_id using `enaho_nchild', keep(1 3) nogen
	
merge 1:1 hogar_id using `enaho_age_partner', keep(1 3) nogen



gen hh_inc_couple_19 = hh_income_couples if period == "2019"
gen hh_inc_couple_q2 = hh_income_couples if period == "2020Q2"
gen hh_inc_couple_q3 = hh_income_couples if period == "2020Q3"

keep nconglome  conglome vivienda hogar ubigeo dominio estrato hogar_id person_id p207 p208a p301a mieperho hh_income_couples hh_inc_couple_*  fac* period p208_partner n_children

save "$data_folder/enaho_2019_descriptive_couples.dta", replace

